package by.alex.dzeshko.facebooksuperclient.adapter;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import by.alex.dzeshko.facebooksuperclient.ImageActivity;
import by.alex.dzeshko.facebooksuperclient.R;
import by.alex.dzeshko.facebooksuperclient.adapter.FriendsListCursorAdapter.ViewHolder;
import by.alex.dzeshko.facebooksuperclient.constants.FbConstants;
import by.alex.dzeshko.facebooksuperclient.image.ImageLoader;
import by.alex.dzeshko.facebooksuperclient.providers.contract.FbContract;
import by.alex.dzeshko.facebooksuperclient.utils.FbPictureManager;

public class FeedCursorAdapter extends CursorAdapter {
	
	private static final String LOG_TAG = "FeedCursorAdapter";

	public FeedCursorAdapter(Context context, Cursor c) {
		super(context, c, true);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (!mDataValid) {
			throw new IllegalStateException("this should only be called when the cursor is valid");
		}
		if (!mCursor.moveToPosition(position)) {
			throw new IllegalStateException("couldn't move cursor to position " + position);
		}
		View v;
		if (convertView == null) {
			v = newView(mContext, mCursor, parent);
			ViewHolder holder = new ViewHolder();
			holder.tvUserName = (TextView) v.findViewById(R.id.tvUserName);
			holder.tvStory = (TextView) v.findViewById(R.id.tvFeedStory);
			holder.tvCreatedDate = (TextView) v.findViewById(R.id.tvFeedDate);
			holder.tvMessage = (TextView) v.findViewById(R.id.tvFeedMessage);
			holder.tvLikesCount = (TextView) v.findViewById(R.id.tvFeedLikesCount);
			holder.tvCommentsCount = (TextView) v.findViewById(R.id.tvFeedCommentsCount);
			holder.tvCaption = (TextView) v.findViewById(R.id.tvFeedCaption);
			holder.ivUserImage = (ImageView) v.findViewById(R.id.ivFeedUserImage);
			holder.ivContentImage = (ImageView) v.findViewById(R.id.ivFeedContentImage);
			v.setTag(R.string.viewHolder, holder);
		} else {
			v = convertView;
		}
		bindView(v, mContext, mCursor);
		return v;
	}

	@Override
	public void bindView(View view, final Context context, Cursor cursor) {
		// checking for tag to avoid entrance headers and footers to adapter
		if(view.getTag(R.string.tag)!=null){
			return;
		}
		ViewHolder holder = (ViewHolder) view.getTag(R.string.viewHolder);
		String name = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.FROM_NAME));
		holder.tvUserName.setText(name);
		String story = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.STORY));
		if(TextUtils.isEmpty(story)) 
			holder.tvStory.setVisibility(View.GONE);
		else {
			holder.tvStory.setText(story);
			holder.tvStory.setVisibility(View.VISIBLE);
		}
		String time = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.CREATED_TIME));
		String[] datetime = time.split("T");
		time = datetime[1].substring(0,datetime[1].indexOf("+"));
		holder.tvCreatedDate.setText(datetime[0]+" at "+time);
		String message = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.MESSAGE));
		if(TextUtils.isEmpty(message)) 
			holder.tvMessage.setVisibility(View.GONE);
		else {
			holder.tvMessage.setText(message);
			holder.tvMessage.setVisibility(View.VISIBLE);
		}
		holder.tvLikesCount.setText(cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.LIKES_COUNT)));
		holder.tvCommentsCount.setText(cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.COMMENTS_COUNT)));
		holder.tvCaption.setText(cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.CAPTION)));
		final String photo_url = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.CONTENT_PICTURE));
		holder.ivContentImage.setVisibility(View.GONE);
		ImageLoader loader =  ImageLoader.getInstance();
		if (!TextUtils.isEmpty(photo_url)){
			holder.ivContentImage.setVisibility(View.VISIBLE);
			//holder.ivContentImage.setTag("content");
			holder.ivContentImage.setImageResource(android.R.drawable.ic_menu_gallery);
			holder.ivContentImage.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View view) {
					Log.d(LOG_TAG, "onItemClick; "+photo_url);
					if(!TextUtils.isEmpty(photo_url)){
						Intent picture = new Intent(context, ImageActivity.class);
						picture.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						picture.putExtra(FbConstants.URL, photo_url);
						context.startActivity(picture);
						return;
					}
				}
			});
			loader.bind(this, holder.ivContentImage, FbPictureManager.getMediumPictureUrl(photo_url));
		}
		String url = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.USER_PICTURE_URL));
		if (!TextUtils.isEmpty(url)){
			holder.ivUserImage.setImageResource(R.drawable.com_facebook_profile_default_icon);
			loader.bind(this, holder.ivUserImage, url);
		}
	}

	@Override
	public View newView(Context ctx, Cursor cursor, ViewGroup root) {
		View view = View.inflate(root.getContext(), R.layout.adapter_feed, null);
		return view;
	}

	static class ViewHolder{
		TextView tvUserName, tvStory, tvCreatedDate, tvMessage, tvLikesCount, tvCommentsCount, tvCaption;
		ImageView ivUserImage, ivContentImage;
	}
	/*public void bindView(View view, final Context context, Cursor cursor) {
		Log.d(LOG_TAG, "bindView");
		// checking for tag to avoid entrance headers and footers to adapter
		if(view.getTag()!=null){
			return;
		}
		TextView tvUserName = (TextView) view.findViewById(R.id.tvUserName);
		TextView tvStory = (TextView) view.findViewById(R.id.tvFeedStory);
		TextView tvCreatedDate = (TextView) view.findViewById(R.id.tvFeedDate);
		TextView tvMessage = (TextView) view.findViewById(R.id.tvFeedMessage);
		TextView tvLikesCount = (TextView) view.findViewById(R.id.tvFeedLikesCount);
		TextView tvCommentsCount = (TextView) view.findViewById(R.id.tvFeedCommentsCount);
		TextView tvCaption = (TextView) view.findViewById(R.id.tvFeedCaption);
		ImageView ivUserImage = (ImageView) view.findViewById(R.id.ivFeedUserImage);
		ImageView ivContentImage = (ImageView) view.findViewById(R.id.ivFeedContentImage);
		
		String name = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.FROM_NAME));
		tvUserName.setText(name);
		String story = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.STORY));
		if(TextUtils.isEmpty(story)) 
			tvStory.setVisibility(View.GONE);
		else {
			tvStory.setText(story);
			tvStory.setVisibility(View.VISIBLE);
		}
		String time = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.CREATED_TIME));
		String[] datetime = time.split("T");
		time = datetime[1].substring(0,datetime[1].indexOf("+"));
		tvCreatedDate.setText(datetime[0]+" at "+time);
		String message = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.MESSAGE));
		if(TextUtils.isEmpty(message)) 
			tvMessage.setVisibility(View.GONE);
		else {
			tvMessage.setText(message);
			tvMessage.setVisibility(View.VISIBLE);
		}
		tvLikesCount.setText(cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.LIKES_COUNT)));
		tvCommentsCount.setText(cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.COMMENTS_COUNT)));
		tvCaption.setText(cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.CAPTION)));
		final String photo_url = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.CONTENT_PICTURE));
		ivContentImage.setVisibility(View.GONE);
		ImageLoader2 loader =  ImageLoader2.getInstance();
		if (!TextUtils.isEmpty(photo_url)){
			ivContentImage.setVisibility(View.VISIBLE);
			ivContentImage.setTag("content");
			ivContentImage.setImageResource(android.R.drawable.ic_menu_gallery);
			ivContentImage.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View view) {
					Log.d(LOG_TAG, "onItemClick; "+photo_url);
					if(!TextUtils.isEmpty(photo_url)){
						Intent picture = new Intent(context, ImageActivity.class);
						picture.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						picture.putExtra(FbConstants.URL, photo_url);
						context.startActivity(picture);
						return;
					}
				}
			});
			loader.bind(this, ivContentImage, FbPictureManager.getMediumPictureUrl(photo_url));
		}
		String url = cursor.getString(cursor.getColumnIndex(FbContract.FeedColumns.USER_PICTURE_URL));
		if (!TextUtils.isEmpty(url)){
			ivUserImage.setImageResource(R.drawable.com_facebook_profile_default_icon);
			loader.bind(this, ivUserImage, url);
		}
	}*/

}
